package dao;

import DBUtil.ConnectionDemo;
import POJO.MoviePerformer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther 尚智江
 * @Date 2022/5/5 20:28
 */
public class MoviePerformerDAOImpl implements MoviePerformerDAO {
    private Connection connection;

// 根据 id 查电影信息
    @Override
    public MoviePerformer queryById(Integer id) throws SQLException{
        MoviePerformer moviePerformer = null;
        connection = ConnectionDemo.getConnection();
//        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from performer where id = ?");
            preparedStatement.setInt(1,id);
            ResultSet rs = preparedStatement.executeQuery();
            while(rs.next()){
                moviePerformer = new MoviePerformer();
                moviePerformer.setId(rs.getInt(1));
                moviePerformer.setMovieTitle(rs.getString(2));
                moviePerformer.setName(rs.getString(3));
                moviePerformer.setRepresentive(rs.getString(4));
            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        return moviePerformer;
    }
// 根据电影名称查找电影信息
    @Override
    public List<MoviePerformer> queryByName(String name) {
        List<MoviePerformer> list = new ArrayList<MoviePerformer>();
        MoviePerformer moviePerformer = null;
        connection = ConnectionDemo.getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from performer where name = ?");
            preparedStatement.setString(1,name);
            ResultSet rs = preparedStatement.executeQuery();

            while(rs.next()){
                moviePerformer = new MoviePerformer();
                moviePerformer.setId(rs.getInt(1));
                moviePerformer.setMovieTitle(rs.getString(2));
                moviePerformer.setName(rs.getString(3));
                moviePerformer.setRepresentive(rs.getString(4));
                list.add(moviePerformer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
    // 增加信息
    public int insert(MoviePerformer moviePerformer){
        connection =  ConnectionDemo.getConnection();
        int num = 0;
        try{
            String sql ="INSERT INTO performer(id,movieTitle,name,representive)" +
                    "VALUES("
                    + moviePerformer.getId() +",'"
                    + moviePerformer.getMovieTitle() +"','"
                    + moviePerformer.getName() +"','"
                    + moviePerformer.getRepresentive()+"')";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            num = preparedStatement.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return num;
    }
    // 根据id修改表内容
    public int update(MoviePerformer moviePerformer){
        connection = ConnectionDemo.getConnection();
        int num = 0;
        try{
            // 获得数据连接
            String sql = "UPDATE performer SET movieTitle = '"
                    + moviePerformer.getMovieTitle()+"',name = '"
                    + moviePerformer.getName() + "',representive = '"
                    + moviePerformer.getRepresentive()
                    + "'WHERE id = "+ moviePerformer.getId();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
             num = preparedStatement.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return num;
    }
}
